package com.rupertjones.globalcron.domain;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import java.util.Date;

/**
 * <p>&copy Rupert Jones 2011</p>
 *
 * @author rup
 */
@Entity
@Table(name = "job_exec_log")
public class JobExecutionLog extends BaseEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;

    @Column(name = "output")
    private String output = null;

    @Column(name = "last_modified")
    private Date lastModified = null;

    @ManyToOne
    @JoinColumn(name = "job")
    private JobDescriptor job = null;

    @Column(name = "time_taken")
    private long timeTaken;

    @Column(name = "executed_normally")
    private boolean executedNormally;

    public JobExecutionLog() {
    }

    public JobDescriptor getJob() {
        return job;
    }

    public void setJob(JobDescriptor job) {
        this.job = job;
    }

    public Date getLastModified() {
        return lastModified;
    }

    public void setLastModified(Date lastModified) {
        this.lastModified = lastModified;
    }

    public long getTimeTaken() {
        return timeTaken;
    }

    public void setTimeTaken(long timeTaken) {
        this.timeTaken = timeTaken;
    }

    public String getOutput() {
        return output;
    }

    public String getLimitedOutput() {
        if (output != null && output.length() > 50) {
            return output.substring(0, 50).concat("...");
        }
        return output;
    }

    public void setOutput(String output) {
        this.output = output;
    }

    public boolean isHasOutput() {
        return getOutput() != null && getOutput().length() > 0;
    }

    @Override
    public void setId(int id) {
        this.id = id;
    }

    @Override
    public int getId() {
        return this.id;
    }

    public void setExecutedNormally(boolean executedNormally) {
        this.executedNormally = executedNormally;
    }

    public boolean getExecutedNormally() {
        return executedNormally;
    }
}
